// #ifdef H5
const jweixin = require('jweixin-module')
// #endif
const config = require('../../../utils/request/config.js').default

function shareFun(code, href, title, summary, imageUrl, mode) {
	let scene
	let provider
	switch (code) {
		case 'wx':
			provider = 'weixin'
			scene = 'WXSceneSession'
			break;
		case 'pyq':
			provider = 'weixin'
			scene = 'WXSceneTimeline'
			break;
		case 'qq':
			provider = 'qq'
			scene = 'WXSceneSession'
			break;
		case 'qzong':
			provider = 'qq'
			scene = 'WXSceneTimeline'
			break;
	}
	let shareData = {}
	if(mode == 0){
		shareData = {
			href: href,
			title: title,
			summary: summary,
			imageUrl: imageUrl,
		}
	}
	if(mode == 1){
		shareData = {
			summary: summary,
		}
	}
	if(mode == 2){
		shareData = {
			imageUrl: imageUrl,
		}
	}
	if(mode == 3 || mode == 4){
		shareData = {
			mediaUrl: imageUrl
		}
	}
	// #ifdef APP-PLUS
	if(!title || !href || !summary || !imageUrl){
		uni.showModal({
			title:'提示',
			content:'分享参数缺失，请检查'
		})
		return false;
	}
	uni.share({
		provider:provider,
		scene: scene,
		type: mode,
		...shareData,
		success: function(res) {
			uni.showToast({
				title:'分享成功！',
				icon:'none'
			})
			return true;
		},
		fail: function(err) {
			console.log("fail:" + JSON.stringify(err));
			return false;
		}
	});
	// #endif
	// #ifdef H5
	if(!config.wxOfficalAppid || !config.getSignMethod){
		uni.showModal({
			title:'提示',
			content:'未配置公众号appid或签名接口！请检查config配置！'
		})
		return false;
	}
	let url = getSignUrl()
	let method = config.getSignMethod
	uni.request({
		url:config.baseUrl + method,
		method:'GET',
		data:{
			url: encodeURIComponent(url)
		},
		success: (res) => {
			if (res.code == 200) {
				jweixin.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: config.wxOfficalAppid, // 必填，公众号的唯一标识
					timestamp: res.data.timestamp, // 必填，生成签名的时间戳
					nonceStr: res.data.noncestr, // 必填，生成签名的随机串
					signature: res.data.signature, // 必填，签名
					jsApiList: ['updateAppMessageShareData',
						'updateTimelineShareData'] // 必填，需要使用的JS接口列表
				});
				
				jweixin.checkJsApi({
					jsApiList:['updateAppMessageShareData',
						'updateTimelineShareData'],
					success:(res) => {
						jweixin.ready(function() { //需在用户可能点击分享按钮前就先调用
							// 分享给微信好友或QQ好友
							jweixin.updateAppMessageShareData({
								title: title, // 分享标题
								desc: summary, // 分享描述
								link: href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
								imgUrl:imageUrl, // 分享图标
								success: function() {
									// 设置成功
									return true;
								}
							})
						
							// 分享到微信朋友圈或QQ空间
							jweixin.updateTimelineShareData({
								title: title, // 分享标题
								link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
								imgUrl: imageUrl, // 分享图标
								success: function() {
									// 设置成功
									return true;
								}
							})
						});
					}
				})
			}
		}
	})
	// #endif
}

function getSignUrl(){
	var signLink = ''
	var ua = navigator.userAgent.toLowerCase();
	if (/iphone|ipad|ipod/.test(ua)) {
		signLink = uni.getStorageSync('WxCodeUrl');
		if (!signLink) {
			if(window.location.href.includes('/#/')){
				signLink = window.location.href.split('/#/')[0]
			}else{
				signLink = window.location.href.split('/pages')[0]
			}
		}
	} else {
		if(window.location.href.includes('/#/')){
			signLink = window.location.href.split('/#/')[0]
		}else{
			signLink = window.location.href.split('/pages')[0]
		}
	}
	return signLink;
}

module.exports = shareFun;